//
//  UIButton+DFExtension.m
//  Pods
//
//  Created by rujia chen on 17/5/11.
//
//

#import "UIButton+DFExtension.h"
#import "Aspects.h"
#import <objc/runtime.h>
#import "NSString+DFExtension.h"

const char *kPropertyImagePositionType = "kPropertyImagePositionType";
const char *kPropertySpacing = "kPropertySpacing";
@implementation UIButton (DFExtension)

+ (void)load {
    void(^resetImagePositionBlock)(id<AspectInfo>) = ^(id<AspectInfo> info){
        UIButton *button = [info instance];
        [button df_setImagePositionWithType:[button df_imagePositionType] spacing:[button df_spacing]];
    };
    
    [self aspect_hookSelector:@selector(setTitle:forState:) withOptions:AspectPositionAfter usingBlock:resetImagePositionBlock error:nil];
    
    [self aspect_hookSelector:@selector(setImage:forState:) withOptions:AspectPositionAfter usingBlock:resetImagePositionBlock error:nil];
}

- (DFImagePositionType)df_imagePositionType {
    return [objc_getAssociatedObject(self, &kPropertyImagePositionType) integerValue];
}

- (CGFloat)df_spacing {
    return [objc_getAssociatedObject(self, &kPropertySpacing) doubleValue];
}

- (void)df_setImagePositionWithType:(DFImagePositionType)type spacing:(CGFloat)spacing {
    objc_setAssociatedObject(self, &kPropertyImagePositionType, @(type), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, &kPropertySpacing, @(spacing), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    CGSize imageSize = [self imageForState:UIControlStateNormal].size;
    CGSize titleSize = [[self titleForState:UIControlStateNormal] df_sizeWithFont:self.titleLabel.font];
    
    CGSize size = [self sizeThatFits:CGSizeZero];
    
    switch (type) {
        case DFImagePositionTypeLeft: {
            CGFloat delta = spacing / 2.f;
            self.imageEdgeInsets = UIEdgeInsetsMake(0, - delta, 0, delta);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, delta, 0, - delta);
            
            self.contentEdgeInsets = UIEdgeInsetsMake(0, delta, 0, delta);
            break;
        }
        case DFImagePositionTypeRight: {
            self.titleEdgeInsets = UIEdgeInsetsMake(0, - (imageSize.width + spacing), 0, imageSize.width);
            self.imageEdgeInsets = UIEdgeInsetsMake(0, titleSize.width, 0, - (titleSize.width + spacing));
            
            self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
            break;
        }
        case DFImagePositionTypeTop: {
            self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (imageSize.height + spacing), 0);
            self.imageEdgeInsets = UIEdgeInsetsMake(- (titleSize.height + spacing), 0, 0, - titleSize.width);
            
            CGFloat paddingHorizonal = MAX(titleSize.width, imageSize.width) / 2.f - size.width / 2.f;
            CGFloat paddingVertical = MIN(imageSize.height, titleSize.height) / 2.f + spacing / 2.f;
            self.contentEdgeInsets = UIEdgeInsetsMake(paddingVertical, paddingHorizonal, paddingVertical, paddingHorizonal);
            break;
        }
        case DFImagePositionTypeBottom: {
            self.titleEdgeInsets = UIEdgeInsetsMake(- (imageSize.height + spacing), - imageSize.width, 0, 0);
            self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, - (titleSize.height + spacing), - titleSize.width);
            
            CGFloat paddingHorizonal = MAX(titleSize.width, imageSize.width) / 2.f - size.width / 2.f;
            CGFloat paddingVertical = MIN(imageSize.height, titleSize.height) / 2.f + spacing / 2.f;
            self.contentEdgeInsets = UIEdgeInsetsMake(paddingVertical, paddingHorizonal, paddingVertical, paddingHorizonal);
            break;
        }
    }
}

@end
